package com.magicode.cms.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.magicode.cms.dao.INewsDao;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.Tag;
import com.magicode.cms.entiy.base.User;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.portal.common.assist.news.TagNewInfo;

@Repository("newsDao")
public class NewsDaoImpl implements INewsDao {

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Override
	public List<News> list() {
		String hql = " from com.magicode.cms.entiy.base.News t ";
		List list = commonDaoService.getListByHQL(hql, null);
		return list;
	}

	@Override
	public News get(String id) {
		News news = (News) commonDaoService.get(News.class, id);
		return news;
	}

	@Override
	public void delete(String id) {
		News news = this.get(id);
		commonDaoService.delete(news);
	}

	@Override
	public News save(News news) {
		commonDaoService.save(news);
		return news;
	}

	@Override
	public News update(News news) {
		commonDaoService.update(news);
		return news;
	}

	@Override
	public List<Map<String, Object>> getTagsByNewsId(String newsid) {

		List<Map<String, Object>> list = null;
		String sqlString = " select new map(tag.id as id , tag.title as title ) from com.magicode.cms.entiy.base.Tag tag , com.magicode.cms.entiy.base.News n where "
				+ " tag in elements(n.tags) and n.id ='"+newsid+"' " ;
		list = commonDaoService.getListByHQL(sqlString);
		
		return list;
		
		
	}
	/**
	 * 获取标签 资讯统计相关信息
	 * @param tagid
	 * @return
	 */
	@Override
	public TagNewInfo getTagNewInfo(String tagid) {
		TagNewInfo reslt = new TagNewInfo();
		reslt.setTagid(tagid);
		Tag tag =(Tag) commonDaoService.get(Tag.class, tagid);
		reslt.setTagTitle(tag.getTitle());
		
		return reslt;
	}
}
